#include<stdio.h>
#include<fcntl.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/select.h>
#include<sys/stat.h>
#include<sys/time.h>

//同时监听鼠标和标准输入
//IO多路复用
int fd =-1;
void handler(int signum){

    char buf[64] = {0};
    int ret = read(fd,buf,64);
    if(ret < 0){
        perror("read err");
        return;
    }else{
        printf("len = %d\n",ret);
        // printf("8888");
    }

}

int main(void){


    fd=open("/dev/input/mouse0",O_RDONLY);
    if(fd < 0){
        perror("open err:");
        return -1;
    }
    fd_set rdfds;//创建空表
    int maxfd = fd;

    char buf[64] = {0};

    FD_ZERO(&rdfds);//清处表
    FD_SET(fd,&rdfds);//添加文件描述符fd
    FD_SET(0,&rdfds);//添加文件描述符0
    
    struct timeval tv;
    tv.tv_sec=3;
    tv.tv_usec=0;

    while(1){
        FD_ZERO(&rdfds);//清处表
        FD_SET(0,&rdfds);//添加文件描述符0
        FD_SET(fd,&rdfds);//添加文件描述符fd

        tv.tv_sec=3;
        tv.tv_usec=0;
        int ret = select(maxfd+1,&rdfds,NULL,NULL,&tv);
        if(ret < 0){
            perror("select err");
            continue;
        }else if(ret > 0){
            if(FD_ISSET(0,&rdfds)){
                printf("at");
                gets(buf);
                printf("buf = %s\n",buf);
            }
            if(FD_ISSET(fd,&rdfds)){
                int ret = read(fd,buf,64);
                printf("len = %d\n",ret);
            }
        }else{
            printf("超时\n");
        }
    }


    return 0;
}